package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {
    /**
     * 随机生成4个0-9之间的不重复的整型数作为答案并返回的功能
     */
    public static List<Integer> generateAnswer() {
        List<Integer> answer = new ArrayList<>();
        Random random = new Random();
        while (answer.size() < 4) {
            int num = random.nextInt(10);
            if (!answer.contains(num)) {
                answer.add(num);
            }
        }
        return answer;
    }

    /**
     * 判断数字是否都在0-9之间
     *
     * @param num
     * @return
     */
    public static boolean isBetween0And9(int num) {
        return num >= 0 && num <= 9;
    }

    /**
     * 接收玩家输入的4个数字并返回
     */
    public static List<Integer> getPlayerInput() {
        List<Integer> input = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入4个数字：");
        System.out.println("请输入：");
        while (input.size() < 4) {
            int num = scanner.nextInt();
            if (num < 0 || num > 9) {
                System.out.println("输入数字必须在0-9之间，请重新输入：");
                continue;
            }
            if (input.contains(num)) {
                System.out.println("输入数字不能重复，请重新输入：");
                continue;
            }
            input.add(num);
        }
        return input;
    }

    /**
     * 提示信息
     */
    public static int compareGuessAnswer(List<Integer> playerInput, List<Integer> answer) {
        // 位置和数字都正确的个数
        int aCount = 0;
        System.out.println("提示：");
        for (int i = 0; i < playerInput.size(); i++) {
            int a = playerInput.get(i);
            int b = answer.get(i);
            if (a == b) {
                aCount++;
                System.out.println("是否猜对第" + (i + 1) + "个数字:" + true);
            } else {
                System.out.println("是否猜对第" + (i + 1) + "个数字:" + false);
            }
        }
        return aCount;
    }

    /**
     * 根据提示信息中A的数量判断玩家是否获胜，如果获胜返回true，否则返回false。
     */
    public static boolean isWin(int count) {
        return count == 4;
    }

    /**
     * 经手动测试正确后，将代码提交到本地仓库
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("猜数字小游戏");
        // 有3次机会
        int chances = 3;
        // 获取随机答案
        List<Integer> answerList = generateAnswer();
        while (true) {
            if (chances == 0) {
                System.out.println("很抱歉，您未猜中正确答案，游戏结束！");
                System.out.println("正确答案是：" + answerList);
                System.out.println("再玩一局？是的话请输入：y,输入其它任意字符退出游戏。");
                Scanner scanner = new Scanner(System.in);
                String next = scanner.next();
                if (next.equalsIgnoreCase("y")) {
                    chances = 10;
                    answerList = generateAnswer();
                } else {
                    System.out.println("退出游戏");
                    break;
                }

            }
            // 获取玩家输入
            List<Integer> playerInput = getPlayerInput();
            System.out.println(playerInput);
            // 验证结果
            int count = compareGuessAnswer(playerInput, answerList);
            if (isWin(count)) {
                System.out.println("恭喜你,猜对了！");
                break;
            } else {
                chances--;
                System.out.println("猜错了哦！你还剩下" + chances + "次机会。");
            }
        }
    }
}
